from django.db import models

from jingxi_shop_project.settings import IMAGE_URL


class Goods(models.Model):
    good_id = models.AutoField(primary_key=True, unique=True, verbose_name="商品ID")
    type_id = models.IntegerField(default=1, null=False, verbose_name="商品类型ID")
    sku_id = models.IntegerField(unique=True,null=False, verbose_name="sku_id")
    name = models.CharField(max_length=255, unique=True, null=False, verbose_name='商品名称')
    detail = models.CharField(max_length=255, unique=True, null=False, verbose_name='商品描述')
    price = models.DecimalField(max_digits=10, decimal_places=2, null=False, verbose_name='商品价格')
    stock = models.IntegerField(null=False, verbose_name='商品库存')
    photo_URL = models.CharField(max_length=255, null=False, verbose_name='商品图片路径')

    # def __str__(self):
    #     return f"{self.name} - {self.detail}"

    def to_dict(self):
        return{
            "good_id": self.good_id,
            "type_id": self.type_id,
            "name": self.name,
            "detail": self.detail,
            "price": self,
            "stock": self.stock,
            "photo_URL": IMAGE_URL + self.photo_URL,
        }

    class Meta:
        db_table = 'goods'